﻿using System.Windows.Controls;
using nRoute.Components.Composition;
using nRoute.ViewServices.Contracts;
#if !SILVERLIGHT
using Microsoft.Win32;
#endif

namespace nRoute.ViewServices.Services
{
    [MapViewService(typeof(ISaveFileViewService), SaveFileViewService.SERVICE_NAME,
         InitializationMode = InitializationMode.OnDemand, Lifetime = ViewServiceLifetime.PerInstance)]
    public class SaveFileViewService 
		 : ISaveFileViewService
    {
        private const string SERVICE_NAME = "nRouteSaveFileViewService";
        private const string ALL_FILES_FILTER = "All Files (*.*)|*.*";

        private string _filter = ALL_FILES_FILTER;  // by default
        private int _filterIndex = 1;       // by default, this is important
        
#region ISaveFileViewService Members

        public string Filter
        {
            get { return _filter; }
            set { _filter = value; }
        }

        public int FilterIndex
        {
            get { return _filterIndex; }
            set { _filterIndex = value; }
        }

        public string DefaultExt { get; set; }

        public SaveFileResponse SaveFile()
        {
            var _saveFileDialog = new SaveFileDialog
                                      {
                                          Filter = Filter,
                                          FilterIndex = FilterIndex,
                                          DefaultExt = DefaultExt
                                      };

            // we open and send back the result
            var _result = _saveFileDialog.ShowDialog();
            return !_result.GetValueOrDefault(false) ? new SaveFileResponse(true) : 
                new SaveFileResponse(_saveFileDialog.SafeFileName, _saveFileDialog.OpenFile);
        }

#endregion

    }

}

